package net.atomarrow.pr.bean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.atomarrow.pr.domain.TbRole;
import net.atomarrow.pr.domain.TbRoleMenu;
import net.atomarrow.pr.util.PermissionCheckUtil;

public class PermissionBean {
	private List<Set<String>> roleFuncUrls;
	Map<Integer,List<TbRoleMenu>> roleMenuMap;
	/**
	 * 查询指定的level
	 * @param level
	 * @return
	 */
	public List<TbRoleMenu> getLevel(int level){
		List<TbRoleMenu> levelRoleMenus = new ArrayList<TbRoleMenu>();
		List<TbRole> roles=PermissionCheckUtil.getCurrentAccountRoles();
		for(TbRole role:roles){
		    levelRoleMenus.addAll(getLevel(level, role.getId()));
		}
		return levelRoleMenus;
	}
	
	   /**
     * 查询指定的level
     * @param level
     * @return
     */
    public List<TbRoleMenu> getLevel(int level,int roleId){
        boolean isSafe = PermissionCheckUtil.checkRoleExist(roleId);
        if(isSafe==false){
            return Collections.EMPTY_LIST;
        }
        List<TbRoleMenu> levelRoleMenus = new ArrayList<TbRoleMenu>();
        List<TbRoleMenu> roleMenus = roleMenuMap.get(roleId);
        for(TbRoleMenu menu:roleMenus){
            if(menu.getLevel()==level){
                levelRoleMenus.add(menu);
            }
        }
        return levelRoleMenus;
    }
    
    /**
     * 查询指定level之外的所有level
     * @param level
     * @return
     */
    public List<TbRoleMenu> getLevelWithout(int level){
        List<TbRoleMenu> levelRoleMenus = new ArrayList<TbRoleMenu>();
        List<TbRole> roles=PermissionCheckUtil.getCurrentAccountRoles();
        for(TbRole role:roles){
            levelRoleMenus.addAll(getLevelWithout(level, role.getId()));
        }
        return levelRoleMenus;
    }
	
	/**
	 * 查询指定level之外的所有level
	 * @param level
	 * @return
	 */
	public List<TbRoleMenu> getLevelWithout(int level,int roleId){
        boolean isSafe = PermissionCheckUtil.checkRoleExist(roleId);
        if(isSafe==false){
            return Collections.EMPTY_LIST;
        }
        List<TbRoleMenu> levelRoleMenus = new ArrayList<TbRoleMenu>();
        List<TbRoleMenu> roleMenus = roleMenuMap.get(roleId);
        for(TbRoleMenu menu:roleMenus){
            if(menu.getLevel()!=level){
                levelRoleMenus.add(menu);
            }
        }
        return levelRoleMenus;
    }

	public PermissionBean() {
		roleFuncUrls=new ArrayList<Set<String>>();
	}
	
	public boolean check(String url){
		for(Set<String> set:roleFuncUrls){
			if(set.contains(url)){
				return true;
			}
		}
		return false;
	}
	public void addFunUrlSet(Set<String> set){
		roleFuncUrls.add(set);
	}

    public Map<Integer, List<TbRoleMenu>> getRoleMenuMap() {
        return roleMenuMap;
    }

    public void setRoleMenuMap(Map<Integer, List<TbRoleMenu>> roleMenuMap) {
        this.roleMenuMap = roleMenuMap;
    }
}
